package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.pojo.Affix;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.SellerVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    HttpSession session;

    @Autowired
    private AffixService affixService;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Integer addRoute(RouteVo routeVo) {
        SellerVo sellerVo = (SellerVo) session.getAttribute(session.getId());
        routeVo.setSellerId(sellerVo.getId());
        //保存
        return routeMapper.insert(BeanConv.toBean(routeVo, Route.class));
    }

    @Override
    public Integer updateRoute(RouteVo routeVo) {
        if (EmptyUtil.isNullOrEmpty(routeVo.getId())){
            return 0;
        }
        int flag = routeMapper.updateByPrimaryKey(BeanConv.toBean(routeVo, Route.class));
        return flag;
    }

    @Override
    public RouteVo findRouteById(RouteVo routeVo) {

        RBucket<RouteVo> bucket = redissonClient.getBucket(RedisConstant.ROUTESERVICE_FINDROUTEBYID + routeVo.getId());
        RouteVo resultRouteVo = bucket.get();
        if(!EmptyUtil.isNullOrEmpty(resultRouteVo)) {
            return resultRouteVo;
        }

        Route route = routeMapper.selectByPrimaryKey(routeVo.getId());
        //查询线路包含的图片信息
        AffixVo affixVo = new AffixVo();
        affixVo.setBusinessId(routeVo.getId());
        List<AffixVo> affixVoList = affixService.findAffixByBusinessId(affixVo);
        //将线路包含的图片集合封装到图片集合中
        resultRouteVo = BeanConv.toBean(route, RouteVo.class);
        resultRouteVo.setAffixVoList(affixVoList);
        //设置缓存
        if(!EmptyUtil.isNullOrEmpty(resultRouteVo)) {
            bucket.set(resultRouteVo, 3600, TimeUnit.SECONDS);
        }
        return resultRouteVo;
    }

    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        RouteExample example = new RouteExample();
        RouteExample.Criteria criteria = example.createCriteria();
        //多条件查询
        if (!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%"+routeVo.getRouteName()+"%");
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%"+routeVo.getRouteName()+"%");
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        List<Route> routes = routeMapper.selectByExample(example);
        PageInfo<Route> pageInfo = new PageInfo<>(routes);
        PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
        //外部对象拷贝
        BeanConv.toBean(pageInfo, pageInfoVo);

        //结果集转换
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        for (RouteVo routeVo1 : routeVoList) {
            //查询线路包含的图片信息
            AffixVo affixVo = new AffixVo();
            affixVo.setBusinessId(routeVo.getId());
            List<AffixVo> affixVoList = affixService.findAffixByBusinessId(affixVo);
            //将线路包含的图片集合封装到图片集合中
            routeVo1.setAffixVoList(affixVoList);

        }

        pageInfoVo.setList(routeVoList);
        return pageInfoVo;
    }

}
